#include <TGS/GeoMath2.0/CGAlgorithms.h>
#include <TGS/GeoMath2.0/Point.h>
#include <TGS/GeoMath2.0/Line.h>
#include <cmath>
#include <limits>

namespace TGS{
namespace GeoMath{

double CGAlgorithms::distance(double x0, double y0, double x1, double y1)
{
	double deltaX = x1 - x0;
	double deltaY = y1 - y0;

	return std::sqrt(deltaX * deltaX + deltaY * deltaY);
}

double CGAlgorithms::distance( const Point& point, const Line& line )
{
	double temp = (point.getX() * line.getA()) + (point.getY() * line.getB()) + line.getC();
	double temp2 = sqrt(line.getA() * line.getA() + line.getB() * line.getB());

	return std::abs(temp) / temp2;
}

double CGAlgorithms::distance0(const Point& lhs, const Point& rhs)
{
	return distance(lhs.getX(), lhs.getY(), rhs.getX(), rhs.getY());
}

double CGAlgorithms::azimuth(double startX, double startY, double endX, double endY)
{
	const double PI = 4.0 * std::atan(1.0);
	double angle = std::atan2(endY - startY, endX - startX);
	if(angle < 0.0) angle += 2 * PI;

	return angle;
}

double CGAlgorithms::azimuth0(const Point& startPoint, const Point& endPoint)
{
	return azimuth(startPoint.getX(), startPoint.getY(), endPoint.getX(), endPoint.getY());
}

}
}